/*********************************************************************************
 *      Copyright:  (C) 2022 Zhang xiangyun<1327295677@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  mqtt.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(20/05/22)
 *         Author:  Zhang xiangyun <1327295677@qq.com>
 *      ChangeLog:  1, Release initial version on "20/05/22 21:08:30"
 *                 
 ********************************************************************************/
#include "mqtt.h"
#include <stdio.h>
#include "cJSON.h"
#include "logger.h"
#include <mosquitto.h>
#include <errno.h>
#include <string.h>
#include "cJSON.h"
#include "mqtt.h"

#define PORT 1883


void connect_pub_callback(struct mosquitto *mosq,void *obj,int rc);
int mqtt_connect(mosquitto_t mos_msg);
int mqtt_pub(char *p,mosquitto_t mos_msg);
struct mosquitto         *mosq=NULL;

 
void connect_pub_callback(struct mosquitto *mosq,void *obj,int rc)
{
	log_info("start check up  whether mqtt is connected\n");

	int  reconn_rc;

	if(rc != 0)
	{
		log_debug("connect server failure:%s\n");
		
		reconn_rc=mosquitto_reconnect(mosq);
		if(MOSQ_ERR_SUCCESS == reconn_rc)
		{
			log_debug("reconnect broker success!\n");
		}
	}

		
}

int mqtt_connect(mosquitto_t mos_msg)
{
	int                       mq_rv=-1;
	char                      buf[1024];

	//printf("username:%s\n",mos_msg.username);
	//printf("clientid:%s\n",mos_msg.clientid);
	//printf("password:%s\n",mos_msg.password);
	//printf("host:%s\n",mos_msg.host);
	//printf("topic:%s\n",mos_msg.topic);
	mq_rv=mosquitto_lib_init();

	if(MOSQ_ERR_SUCCESS!=mq_rv)
	{
		log_error("mosquitto_lib_init() error\n");
		return -1;
	}
	log_debug("mosquitto init success!\n");

	/* create new mosquitto client  */
	mosq=mosquitto_new(mos_msg.clientid,true,(void *)&mos_msg);
	if(!mosq)
	{
		log_error("mosquitto new failure: %s\n",strerror(errno));
		//mosquitto_lib_cleanup();
		return -1;
	}
	log_debug("create mosquitto successfuly!\n");

	//mosquitto_connect_callback_set(mosq,pub_conn_callback);
	mq_rv=mosquitto_username_pw_set(mosq,mos_msg.username,mos_msg.password);

	if(MOSQ_ERR_SUCCESS != mq_rv)
	{
		log_error("mosquitto_username_pw_set() failure: %s",strerror(errno));
		//mosquitto_destroy(mosq);
	    //mosquitto_lib_cleanup();
		return -1;
	}
	log_debug("Set user_id and password sucessfully!\n");

	mq_rv=mosquitto_connect(mosq,mos_msg.host,PORT,50);
	if(MOSQ_ERR_SUCCESS !=mq_rv)
	{
		log_error("mosquitto_connect() error!\n");
	    //mosquitto_destroy(mosq);
	    //mosquitto_lib_cleanup();
		return -1;
	}
	log_info("mosquitto connect successfuly\n");
	mosquitto_connect_callback_set(mosq,connect_pub_callback);

	if(MOSQ_ERR_SUCCESS != mosquitto_loop_start(mosq))
	{
		printf("mosquitto_loop_start() failure\n");
		mosquitto_destroy(mosq);
     	mosquitto_lib_cleanup();
		return -1;
	}

	return 0;
}

int mqtt_pub(char *p,mosquitto_t mos_msg)
{
	int mq_rv=-1;

	//printf("message:%s\n",p);
	mq_rv = mosquitto_publish(mosq,NULL,mos_msg.topic,strlen(p)+1,p,0,0);
	printf("Mosquitto Pub\n");
    if(MOSQ_ERR_SUCCESS == mq_rv)
    {
	    printf("Mosquitto Pub success\n");
	    printf("----------------------------------------------\n");
		return 0;
    }
	else
    {
        printf("Mosquitto Pub failure\n");
        printf("----------------------------------------------\n");
        return -1;
    }
}
